//
//  OPCarSourceTableViewController.m
//  NewWorld
//
//  Created by 飞奔的羊 on 16/4/27.
//  Copyright © 2016年 itcast. All rights reserved.
//

#import "OPZJCarSourceTableViewController.h"
#import "OPZJCarSourceTableViewCell.h"
#import "OPZJHighCarTableViewCell.h"
#import "OPZJNewCarTableViewCell.h"
#import "OPZJHighSectionView.h"
#import "OPZJNewCarSection.h"
#import "OPZJHighCar.h"
#import "OPZJNewCar.h"
#import "OPZJFatherTableViewController.h"
#import "OPZJOriginTableViewController.h"
@interface OPZJCarSourceTableViewController ()

//新车的模型数组
@property (nonatomic, strong)NSArray *mnewCarArray;
//高端车的模型数组
@property (nonatomic, strong)NSArray  *highCarLst;

@end

@implementation OPZJCarSourceTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置搜索
    UISearchBar *searchBar = [[UISearchBar alloc]init];
    searchBar.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
    searchBar.placeholder = @"可输入车型价格进行搜索";
    self.tableView.tableHeaderView = searchBar;
    [self loadHighCarList];
    
    
}
//获取数据
-(void)loadHighCarList{
    NSDictionary *param = @{@"accountid":@"1"};
    [OPZJHighCar highCarWithURLString:@"highcarlist.json.php" andParam:param finishedBlock:^(NSArray *HighCarList) {
        self.highCarLst = HighCarList;
        [self.tableView reloadData];
    }];
    
    [OPZJNewCar highCarWithURLString:@"newcarlist.json.php" andParam:param finishedBlock:^(NSArray *HighCarList) {
        self.mnewCarArray = HighCarList;
        [self.tableView reloadData];
    }];
}

//数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (section==0) {
        return 3;
    }else if (section==1){
       
        return self.highCarLst.count;
    }else{
        return self.mnewCarArray.count;
    }
}
//设置组头
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{


    if (section==0) {
        return [[UIView alloc]init];
    }else if (section==1){
        OPZJHighSectionView *highCar = [OPZJHighSectionView headerViewWithTableView:tableView];
        highCar.title  = @"高端品牌";
        return highCar;
    }else{
        OPZJNewCarSection *newCar = [OPZJNewCarSection headerViewWithTableView:tableView];
        newCar.title = @"最新车源";
        return newCar;
        
    }
}
//设置选中
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0&&(indexPath.row==1||indexPath.row==2||indexPath.row==0)) {
//        OPZJOriginTableViewController *ovc =[[ OPZJOriginTableViewController alloc]init];
        OPZJFatherTableViewController *fvc = [[OPZJFatherTableViewController alloc]init];
        fvc.plistName = @"father01.plist";
        [self.navigationController pushViewController:fvc animated:YES];
    }
    
}
//设置组高
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0;
    }else{
        return 30;
    }
}
//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 44;
    }else if (indexPath.section==1){
        return 44;
    }else{
        return 120;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = @"";
    if (indexPath.section==0) {
        str = @"1";
        NSArray *array = @[@"现车",@"热销车型",@"平行进口车"];
        OPZJCarSourceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [OPZJCarSourceTableViewCell sourceCar];
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.title = array[indexPath.row];
        return cell;

    }else if(indexPath.section==1){
        str = @"2";
        OPZJHighCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [OPZJHighCarTableViewCell highCar];
        }
        cell.highCar = self.highCarLst[indexPath.row];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }else{
        str = @"3";
        OPZJNewCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
        if (!cell) {
            cell = [OPZJNewCarTableViewCell newCar];
        }
        cell.carNew = self.mnewCarArray[indexPath.row];
        return cell;
    }
    
    
    
    }


@end
